//
//  UIImage+compression.m
//  Bettle
//
//  Created by 薛小刚 on 2017/9/28.
//  Copyright © 2017年 panhongliu. All rights reserved.
//

#import "UIImage+compression.h"

@implementation UIImage (compression)

- (NSData *) useImage {
    CGFloat hfactor = self.size.width / SCREEN_WIDTH;
    CGFloat vfactor = self.size.height / SCREEN_HEIGHT;
    CGFloat factor = fmax(hfactor, vfactor);
    CGFloat width = self.size.width / factor;
    CGFloat height = self.size.height / factor;
    CGSize nSize = CGSizeMake(width, height);
    UIGraphicsBeginImageContextWithOptions(nSize, NO, 1);
    [self drawInRect:CGRectMake(0, 0, width, height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(scaledImage);
    return data;
}

- (UIImage *) useImageReturnImage {
    CGFloat hfactor = self.size.width / SCREEN_WIDTH;
    CGFloat vfactor = self.size.height / SCREEN_HEIGHT;
    CGFloat factor = fmax(hfactor, vfactor);
    CGFloat width = self.size.width / (factor > 1 ? factor : 1);
    CGFloat height = self.size.height / (factor > 1 ? factor : 1);
    CGSize nSize = CGSizeMake(width, height);
    UIGraphicsBeginImageContextWithOptions(nSize, NO, 1);
    [self drawInRect:CGRectMake(0, 0, width, height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
    
}

+ (UIImage *) getImageWithColor:(UIColor *) color alp:(CGFloat)alp {
    /// 将颜色分为红绿蓝
    CGFloat r, g, b, a;
    
    [color getRed:&r green:&g blue:&b alpha:&a];
    
    
//
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
//    /// 将红绿蓝重新组合
    UIColor * useColor = [UIColor colorWithRed:r green:g blue:b alpha:alp];
    CGContextSetFillColorWithColor(context, [useColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return theImage;
}

- (UIImage*) GetImageWithColor:(UIColor*)color alp:(CGFloat) alp andHeight:(CGFloat)height
{
    
    
    CGFloat r, g, b, a;
    
    [color getRed:&r green:&g blue:&b alpha:&a];
    
    
    //
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    /// 将红绿蓝重新组合
    UIColor * useColor = [UIColor colorWithRed:r green:g blue:b alpha:alp];
    CGContextSetFillColorWithColor(context, [useColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return theImage;
}

+ (NSString *)hexFromUIColor:(UIColor *)color
{
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0]
                                green:components[0]
                                 blue:components[0]
                                alpha:components[1]];
    }
    
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    
    return [NSString stringWithFormat:@"#%x%x%x", (int)((CGColorGetComponents(color.CGColor))[0]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[1]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}

+ (UIImage *) getImageWithString:(NSString *) string {
    CIFilter * cif = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [cif setDefaults];
    NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [cif setValue:data forKeyPath:@"inputMessage"];
    CIImage * ciImage = [cif outputImage];
    
    CGRect extent = CGRectIntegral(ciImage.extent);
    CGFloat scale = 10;
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:ciImage fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

@end
